package com.sa.web.behind.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import net.sf.json.JSONArray;

import com.opensymphony.xwork2.ActionSupport;
import com.sa.web.bean.UserBean;
import com.sa.web.dao.service.UserInfoDao;
import com.sa.web.util.Page;
import com.sa.web.util.Util;

public class UserManageAction extends ActionSupport {
	/** 序列 */
	private static final long serialVersionUID = 1L;
	/** 用户信息dao*/
	private UserInfoDao userInfoDao;
	/** 用户信息bean*/		
	private UserBean userBean;
	/** 设置管理员信息集合 */
	List<UserBean> userList;
	/** easyUi对象*/
	private JSONArray rows;
	/** 信息总数*/
	private JSONArray total;
	/** 获取返回值*/
	private JSONArray msg;
	/** 管理员姓名*/
	private String userName;
	/** 管理员密码*/
	private String password;
	/** 学生ID*/
	private String userId;
	
   /**
    * 初始化管理员查询页面
    * @return success
    * @throws Exception
    */
	public String initUserFind() throws Exception {
		return SUCCESS;
	}
	
	/**
	 * 初始化管理员更新页面
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String initUserInfoUpdate() throws Exception {
		setUserBean((userInfoDao.getUserInfo(Util.getRequest().getParameter("UserId"))));
		return SUCCESS;
	}
	
	/**
	 * 初始化添加管理员页面
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String initUserInfoAdd() throws Exception {
		return SUCCESS;
	}
	
	/**
	 * 添加管理员信息
	 * @return success
	 * @throws Exception
	 */
	public String addUserInfo() throws Exception{
		userBean.setId(Util.getUUID());

		if(StringUtils.isEmpty(userBean.getUsername())){
			msg = Util.returnMsg("用户名不能为空，请重新输入");
		}
		else if(StringUtils.isEmpty(userBean.getPassword())){
			msg = Util.returnMsg("密码不能为空，请重新输入");			
		}
		else if(userBean.getUsername().length()>20){
			msg = Util.returnMsg("用户名不能超过20位，请重新输入");
		}
		else if(userBean.getPassword().length()>20){
			msg = Util.returnMsg("密码不能超过20位，请重新输入");
		}
		else{
		boolean result=	userInfoDao.addUserInfo(userBean);
		msg = result?Util.returnMsg("添加成功"):Util.returnMsg("添加失败");
		}
		
		return SUCCESS;
	}
	
	/**
	 * 修改管理员信息
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String updateUserInfo() throws Exception{
		userInfoDao.updateUserInfo(userBean);
		return SUCCESS;
	}
	
	/**
	 * 查找管理员信息
	 * @return json
	 * @throws Exception
	 */
	public String findUserInfoList()  throws Exception{
		userList = userInfoDao.getUserInfoList( Util.getRequest().getParameter("UserName"),Page.getStarIndex(),Page.getPreIndex());
		 rows = JSONArray.fromObject(userList);
		 total = JSONArray.fromObject(userInfoDao.getUserInfoCount(Util.getRequest().getParameter("UserName")));
		 return Util.jSONSuccess();
	}
	
	
	/**
	 * 删除管理员信息
	 * @return json
	 * @throws Exception
	 */
	public String deleteUserInfo() throws Exception {
		userInfoDao.deleteUserInfo(Util.getRequest().getParameter("UserId")==null?"":Util.getRequest().getParameter("UserId").toString());
		msg = JSONArray.fromObject("[\"删除成功\"]");
		return Util.jSONSuccess();
	}

/**************************getter and setter****************************************************************************************************/
	
	public UserInfoDao getUserInfoDao() {
		return userInfoDao;
	}

	public void setUserInfoDao(UserInfoDao UserInfoDao) {
		this.userInfoDao = UserInfoDao;
	}

	public UserBean getUserBean() {
		return userBean;
	}

	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}

	public List<UserBean> getUserList() {
		return userList;
	}

	public void setUserList(List<UserBean> UserList) {
		this.userList = userList;
	}

	public JSONArray getTotal() {
		return total;
	}

	public void setTotal(JSONArray total) {
		this.total = total;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String UserName) {
		this.userName = UserName;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String UserId) {
		this.userId = UserId;
	}

	public JSONArray getRows() {
		return rows;
	}

	public JSONArray getMsg() {
		return msg;
	}
	
	
}
